package com.google.code.sharecontent.action;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.log.Log;

import com.google.code.sharecontent.DBConnect;
import com.google.code.sharecontent.dao.ContentDao;
import com.google.code.sharecontent.dao.ContentsArtistsViewDao;
import com.google.code.sharecontent.dao.PlaylistContentDao;
import com.google.code.sharecontent.dao.PlaylistDao;
import com.google.code.sharecontent.dao.ResourceDao;
import com.google.code.sharecontent.dao.ResourceLogDao;
import com.google.code.sharecontent.dao.StatusDao;
import com.google.code.sharecontent.dao.TypeDao;
import com.google.code.sharecontent.dao.UserDao;
import com.google.code.sharecontent.dao.impl.ContentDaoImpl;
import com.google.code.sharecontent.dao.impl.ContentsArtistsViewDaoImpl;
import com.google.code.sharecontent.dao.impl.PlaylistContentDaoImpl;
import com.google.code.sharecontent.dao.impl.PlaylistDaoImpl;
import com.google.code.sharecontent.dao.impl.ResourceDaoImpl;
import com.google.code.sharecontent.dao.impl.ResourceLogDaoImpl;
import com.google.code.sharecontent.dao.impl.StatusDaoImpl;
import com.google.code.sharecontent.dao.impl.TypeDaoImpl;
import com.google.code.sharecontent.dao.impl.UserDaoImpl;
import com.google.code.sharecontent.model.Status;

@Name("daoAction")
@Scope(ScopeType.APPLICATION)
@Startup
public class DaoAction {

	private Connection conn;
	private DBConnect bdConnect;
	
	private UserDao userDao;
	private StatusDao statusDao;
	private ResourceDao resourceDao;
	private ResourceLogDao resourceLogDao;
	private ContentDao contentDao;
	private TypeDao typeDao;
	private PlaylistDao playlistDao;
	private PlaylistContentDao playlistContentDao;
	private ContentsArtistsViewDao contentsArtistsViewDao;
	
	@Logger
	Log log;
	
	@Create
	public void start() {
		getConnection();
	}
	
	@Destroy
	public void stop() {
		log.info("close Connection");
		closeConnection();
		this.conn = null;
	}
	
	private void closeConnection() {
		if (bdConnect != null) {
			bdConnect.close();
		}
	}
	
	public Connection getConnection() {
		if (this.conn == null) {
			log.info("Init DB connection");
			bdConnect = new DBConnect();
			try {
				bdConnect.init("");
				this.conn = bdConnect.getConnection();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.conn;
	}


	public UserDao getUserDao() {
		if (this.userDao == null) {
			this.userDao = new UserDaoImpl(getConnection());
		}
		return this.userDao;
	}

	public StatusDao getStatusDao() {
		if (this.statusDao == null) {
			this.statusDao = new StatusDaoImpl(getConnection());
		}
		return this.statusDao;
	}
	
	public ResourceDao getResourceDao() {
		if (this.resourceDao == null) {
			this.resourceDao = new ResourceDaoImpl(getConnection());
		}
		return this.resourceDao;
	}
	
	public ResourceLogDao getResourceLogDao() {
		if (this.resourceLogDao == null) {
			this.resourceLogDao = new ResourceLogDaoImpl(getConnection());
		}
		return this.resourceLogDao;
	}
	
	public ContentDao getContentDao() {
		if (this.contentDao == null) {
			this.contentDao = new ContentDaoImpl(getConnection());
		}
		return this.contentDao;
	}
	
	public TypeDao getTypeDao() {
		if (this.typeDao == null) {
			this.typeDao = new TypeDaoImpl(getConnection());
		}
		return this.typeDao;
	}
	
	public PlaylistDao getPlaylistDao() {
		if (this.playlistDao == null) {
			this.playlistDao = new PlaylistDaoImpl(getConnection());
		}
		return this.playlistDao;
	}
	
	public PlaylistContentDao getPlaylistContentDao() {
		if (this.playlistContentDao == null) {
			this.playlistContentDao = new PlaylistContentDaoImpl(getConnection());
		}
		return this.playlistContentDao;

	}
	
	public ContentsArtistsViewDao getContentsArtistsViewDao() {
		if (this.contentsArtistsViewDao == null) {
			this.contentsArtistsViewDao = new ContentsArtistsViewDaoImpl(getConnection());
		}
		return this.contentsArtistsViewDao;
	}
}
